package com.common.utils;


import org.apache.commons.lang.StringUtils;

import java.util.Base64;

/**
 * @类名 Base64Utils
 * @描述 TODO
 * @版本 1.0
 * @创建人 Lying~
 * @创建时间 2021/12/29 9:37
 * @修改人 Lying~
 * @修改时间 2021/12/29 9:37
 **/
public class Base64Utils {

    /**
     * base64基本加密  （Authorization）
     * @param clientId
     * @param clientSecret
     * @return
     */
    public static String base64Encode(String clientId, String clientSecret) {
        String signatureString = null;
        try {
            String encodeToString = Base64.getEncoder().encodeToString((clientId +":"+clientSecret).getBytes("utf-8"));
            signatureString = "Basic "+encodeToString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return signatureString;
    }

    /**
     * base64基本解密
     * @param signatureString
     * @return
     */
    public static String base64Decode(String signatureString) {
        String decodeStr = null;
        try {
            signatureString = StringUtils.substringAfter(signatureString,"Basic ");
            byte[] encodeToString = Base64.getDecoder().decode((signatureString));
            decodeStr = new String(encodeToString, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decodeStr;
    }

    public static void main(String[] args) {
        String s = Base64Utils.base64Encode("lying", "sssssssxxxxxx");
        String s1 = Base64Utils.base64Decode(s);
        System.out.println(s);
        System.out.println(s1);
    }
}
